package com.study.java.jinjie.jihe.lianxi.two;


import java.util.Iterator;
import java.util.Set;

public class Score {

    private Set<Student> studentSet ;

    public Score(Set<Student> studentSet) {
        this.studentSet = studentSet;
    }

    //打印学生成绩
    public void printStudent() {
        int l = studentSet.size();
        if (studentSet != null&&l>0) {
            System.out.print("序号");
            System.out.print("\t 学号");
            System.out.print(" \t姓名");
            System.out.print(" \t成绩\n");
            // 打印分割线
            System.out.println("----------------------------");
            // 使用迭代器遍历Set并打印元素
             Iterator<Student> iterator = studentSet.iterator();
                while (iterator.hasNext()) {
                    for (int i = 0; i < l; i++) {
                    Student element = iterator.next();
                    // 解析学生信息 打印
                    System.out.print(i+1);  // 打印序号
                    System.out.print(" \t" + element.getNum());
                    System.out.print(" \t" + element.getName());
                    System.out.print(" \t" + element.getScore() + "\n");
                    // 打印分割线
                    System.out.println("----------------------------");
                }
            }
        }
    }
    //  平均成绩
    public int Pingjun() {
        int pingjun = 0;
        int l = studentSet.size();
        Iterator<Student> iterator = studentSet.iterator();
        if (studentSet != null && l > 0) {
            while (iterator.hasNext()) {
                for (int i = 0; i < l; i++) {
                    Student element = iterator.next();
                    pingjun += element.getScore();
                }
                pingjun = pingjun / l;
            }
            System.out.println("平均成绩:" + pingjun);
        }
        return pingjun;
    }
    //  最高成绩
    public  String  zuigao( ){

        short max = 0;
        String s = " ";
        int l = studentSet.size();
        Iterator<Student> iterator = studentSet.iterator();
        if (studentSet != null && l > 0) {
            while (iterator.hasNext()) {
                Student element = iterator.next();
                if ( element.getScore()>max){
                    max = element.getScore();
                    s = element.getName();
                }
            }
            System.out.println("成绩最高的学生:" + s);
        }
        return s ;
    }




}
